home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / solaris2 / jdk / src / java / awt / checkbo0.jav < prev    next >
Encoding:
Text File  |  1995-10-30  |  4.5 KB  |  178 lines

  1. /*
  2.  * @(#)Checkbox.java    1.11 95/08/17 Sami Shaio
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19. package java.awt;
  20.  
  21. import java.awt.peer.CheckboxPeer;
  22.  
  23. /**
  24.  * A Checkbox object is a graphical user interface element that has a boolean 
  25.  * state.
  26.  *
  27.  * @version    1.11 08/17/95
  28.  * @author     Sami Shaio
  29.  */
  30. public class Checkbox extends Component {
  31.     /**
  32.      * The label of the Checkbox.
  33.      */
  34.     String label;
  35.  
  36.     /**
  37.      * The state of the Checkbox.
  38.      */
  39.     boolean state;
  40.  
  41.     /**
  42.      * The check box group.
  43.      */
  44.     CheckboxGroup group;
  45.  
  46.     /**
  47.      * Constructs a Checkbox with no label, no Checkbox group, and initialized  
  48.      * to a false state.
  49.      */
  50.     public Checkbox() {
  51.     }
  52.  
  53.     /**
  54.      * Constructs a Checkbox with the specified label, no Checkbox group, and 
  55.      * initialized to a false state.
  56.      * @param label the leabel on the Checkbox
  57.      */
  58.     public Checkbox(String label) {
  59.     this.label = label;
  60.     }
  61.  
  62.     /**
  63.      * Constructs a Checkbox with the specified label, specified Checkbox 
  64.      * group, and specified boolean state.  If the specified CheckboxGroup
  65.      * is not equal to null, then this Checkbox becomes a Checkbox button.  
  66.      * If the Checkbox becomes a button, this simply means that only 
  67.      * one Checkbox in a CheckboxGroup may be set at a a time.
  68.      * @param label the label on the Checkbox
  69.      * @param group the CheckboxGroup this Checkbox is in
  70.      * @param state is the initial state of this Checkbox
  71.      */
  72.     public Checkbox(String label, CheckboxGroup group, boolean state) {
  73.     this.label = label;
  74.     this.state = state;
  75.     this.group = group;
  76.     if (state && (group != null)) {
  77.         group.setCurrent(this);
  78.     }
  79.     }
  80.  
  81.     /**
  82.      * Creates the peer of the Checkbox. The peer allows you to change the
  83.      * look of the Checkbox without changing its functionality.
  84.      */
  85.     public synchronized void addNotify() {
  86.     peer = getToolkit().createCheckbox(this);
  87.     super.addNotify();
  88.     }
  89.  
  90.     /**
  91.      * Gets the label of the button.
  92.      * @see #setLabel
  93.      */
  94.     public String getLabel() {
  95.     return label;
  96.     }
  97.  
  98.     /**
  99.      * Sets the button with the specified label.
  100.      * @param label the label of the button
  101.      * @see #getLabel
  102.      */
  103.     public void setLabel(String label) {
  104.     this.label = label;
  105.  
  106.     CheckboxPeer peer = (CheckboxPeer)this.peer;
  107.     if (peer != null) {
  108.         peer.setLabel(label);
  109.     }
  110.     }
  111.  
  112.     /** 
  113.      * Returns the boolean state of the Checkbox. 
  114.      * @see #setState
  115.      */
  116.     public boolean getState() {
  117.     return state;
  118.     }
  119.     
  120.     /** 
  121.      * Sets the Checkbox to the specifed boolean state.
  122.      * @param state the boolean state 
  123.      * @see #getState
  124.      */
  125.     public void setState(boolean state) {
  126.     CheckboxGroup group = this.group;
  127.     if (group != null) {
  128.         if (state) {
  129.         group.setCurrent(this);
  130.         } else if (group.getCurrent() == this) {
  131.         state = true;
  132.         }
  133.     }
  134.     this.state = state;
  135.     CheckboxPeer peer = (CheckboxPeer)this.peer;
  136.     if (peer != null) {
  137.         peer.setState(state);
  138.     }
  139.     }
  140.  
  141.     /**
  142.      * Returns the checkbox group.
  143.      * @see #setCheckboxGroup
  144.      */
  145.     public CheckboxGroup getCheckboxGroup() {
  146.     return group;
  147.     }
  148.  
  149.     /**
  150.      * Sets the CheckboxGroup to the specified group.
  151.      * @param g the new CheckboxGroup
  152.      * @see #getCheckboxGroup
  153.      */
  154.     public void setCheckboxGroup(CheckboxGroup g) {
  155.     CheckboxGroup group = this.group;
  156.     if (group != null) {
  157.         group.setCurrent(null);
  158.     }
  159.     this.group = g;
  160.     CheckboxPeer peer = (CheckboxPeer)this.peer;
  161.     if (peer != null) {
  162.         peer.setCheckboxGroup(g);
  163.     }
  164.     }
  165.  
  166.     /**
  167.      * Returns the parameter String of this Checkbox.
  168.      */
  169.     protected String paramString() {
  170.     String str = super.paramString();
  171.     String label = this.label;
  172.     if (label != null) {
  173.         str += ",label=" + label;
  174.     }
  175.     return str + ",state=" + state;
  176.     }
  177. }
  178.